package cn.jbt.api.admin.service.impl;

import cn.jbt.api.admin.entity.CmsPlaceItem;
import cn.jbt.api.admin.entity.QCmsPlaceItem;
import cn.jbt.api.admin.filter.LoginInfoAccessContext;
import cn.jbt.api.admin.repository.CmsPlaceItemRepository;
import cn.jbt.api.admin.service.ICmsPlaceItemService;
import cn.jbt.common.PageQryParam;
import cn.jbt.common.PageQryResult;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
@AllArgsConstructor
public class CmsPlaceItemServiceImpl implements ICmsPlaceItemService {

    private final JPAQueryFactory jpaQueryFactory;
    private final CmsPlaceItemRepository cmsPlaceItemRepository;

    @Override
    public PageQryResult<CmsPlaceItem> getAllByPage(PageQryParam<CmsPlaceItem> pageQryParam) {
        CmsPlaceItem qParam = pageQryParam.getParam();
        QCmsPlaceItem qCmsPlaceItem = QCmsPlaceItem.cmsPlaceItem;

        Predicate predicate = qCmsPlaceItem.isNotNull().or(qCmsPlaceItem.isNull());
        if (null != qParam) {
            if (!StringUtils.isEmpty(qParam.getTitle())) {
                predicate = ExpressionUtils.and(predicate,
                        qCmsPlaceItem.title.contains(qParam.getTitle()));
            }
            if (null != qParam.getStatus()) {
                predicate = ExpressionUtils.and(predicate,
                        qCmsPlaceItem.status.eq(qParam.getStatus()));
            }
            if (qParam.getPlaceId() > 0) {
                predicate = ExpressionUtils.and(predicate,
                        qCmsPlaceItem.placeId.eq(qParam.getPlaceId()));
            }
        }

        List<CmsPlaceItem> list = jpaQueryFactory.select(qCmsPlaceItem)
                .from(qCmsPlaceItem)
                .where(predicate)
                .orderBy(qCmsPlaceItem.orderby.asc(), qCmsPlaceItem.id.desc())
                .offset((pageQryParam.getPage() - 1) * pageQryParam.getSize())
                .limit(pageQryParam.getSize())
                .fetch();

        long total = Optional.ofNullable(jpaQueryFactory.select(qCmsPlaceItem.id.count())
                .from(qCmsPlaceItem)
                .where(predicate)
                .fetchOne()
        ).orElse(0L);

        return new PageQryResult<>(list, total);
    }

    @Override
    public CmsPlaceItem add(CmsPlaceItem cmsPlaceInfo) {
        cmsPlaceInfo.setCreateBy(LoginInfoAccessContext.getLoginInfo().getNickname());
        cmsPlaceInfo.setUpdateBy(cmsPlaceInfo.getCreateBy());
        return cmsPlaceItemRepository.save(cmsPlaceInfo);
    }

    @Override
    public CmsPlaceItem edit(CmsPlaceItem cmsPlaceInfo) {
        CmsPlaceItem newCmsPlaceItem = cmsPlaceItemRepository.findFirstById(cmsPlaceInfo.getId());
        if (null == newCmsPlaceItem) {
            throw new RuntimeException("404 Notfound");
        }

        newCmsPlaceItem.edit(cmsPlaceInfo);
        newCmsPlaceItem.setUpdateBy(LoginInfoAccessContext.getLoginInfo().getNickname());
        return cmsPlaceItemRepository.save(newCmsPlaceItem);
    }

    @Transactional
    @Override
    public void del(List<Long> ids) {
        cmsPlaceItemRepository.deleteAllByIdInBatch(ids);
    }
}
